﻿
using GCCGrpc.IMServer;

using Grpc.Core;
using Grpc.Net.Client;

using static GCCGrpc.IMServer.IMServerGrpc;

namespace System.IMServer.Client
{
    /// <summary>
    /// IM服务的客户端
    /// </summary>
    public class Manager
    {
        /// <summary>
        /// 所有的服务器客户端
        /// </summary>
        private static readonly List<GrpcChannel> _allServices = new();

        /// <summary>
        /// 初始化服务器列表
        /// </summary>
        /// <param name="services"></param>
        public static void InitIMServers(params string[] services)
        {
            if (_allServices.Count == 0)
            {
                lock (_allServices)
                {
                    if (_allServices.Count == 0)
                    {
                        foreach (var service in services)
                        {
                            _allServices.Add(GrpcChannel.ForAddress(service));
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取所有用户
        /// </summary> 
        /// <param name="currentEventID">当前事件ID</param>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static GetAllUserResponse GetAllUser(string currentEventID, GetAllUserRequest request)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }


            var res = new GetAllUserResponse();
            foreach (var channel in _allServices)
            {
                try
                {
                    var response = new IMServerGrpcClient(channel).GetAllUser(request, metaData);
                    foreach (var uItem in response.Users)
                    {
                        res.Users.Add(uItem);
                    }
                }
                catch
                {
                }
            }
            return res;
        }
        /// <summary>
        /// 给所有人发消息
        /// </summary> 
        /// <param name="currentEventID">当前事件ID</param>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static SendMessageToAllUserResponse SendMessageToAllUser(string currentEventID, SendMessageToAllUserRequest request)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            foreach (var channel in _allServices)
            {
                try
                {
                    _ = new IMServerGrpcClient(channel).SendMessageToAllUser(request, metaData);
                }
                catch
                {
                }

            }
            return new SendMessageToAllUserResponse();
        }

        /// <summary>
        /// 給某个用户发消息
        /// </summary> 
        /// <param name="currentEventID">当前事件ID</param>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static SendMessageToUserResponse SendMessageToUser(string currentEventID, SendMessageToUserRequest request)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            foreach (var channel in _allServices)
            {
                try
                {
                    _ = new IMServerGrpcClient(channel).SendMessageToUser(request, metaData);
                }
                catch
                {
                }
            }
            return new SendMessageToUserResponse();
        }

        /// <summary>
        /// 给某个用户的某个端发消息
        /// </summary> 
        /// <param name="currentEventID">当前事件ID</param>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static SendMessageToUserClientResponse SendMessageToUserClient(string currentEventID, SendMessageToUserClientRequest request)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            foreach (var channel in _allServices)
            {
                try
                {
                    _ = new IMServerGrpcClient(channel).SendMessageToUserClient(request, metaData);
                }
                catch
                {
                }
            }
            return new SendMessageToUserClientResponse();
        }

        /// <summary>
        /// 强制某个人下线,如果clientID不传/空字符串,则是把这个人所有的端都下线
        /// </summary> 
        /// <param name="currentEventID">当前事件ID</param>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public static ForcedOfflineResponse ForcedOffline(string currentEventID, ForcedOfflineRequest request)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }

            foreach (var channel in _allServices)
            {
                try
                {
                    _ = new IMServerGrpcClient(channel).ForcedOffline(request, metaData);
                }
                catch
                {
                }
            }
            return new ForcedOfflineResponse();
        }
    }
}
